const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const PointsRecord = sequelize.define('PointsRecord', {
        id: {
            type: DataTypes.BIGINT,
            primaryKey: true,
            autoIncrement: true,
            comment: '记录ID'
        },
        user_id: {
            type: DataTypes.BIGINT,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        points: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        type: {
            type: DataTypes.TINYINT,
            allowNull: false,
            comment: '1-回收获得 2-兑换消耗'
        },
        order_id: DataTypes.BIGINT,
        description: DataTypes.STRING(255),
        deleted_at: DataTypes.DATE
    }, {
        paranoid: true,
        timestamps: true,
        tableName: 'points_records',
        comment: '积分记录表'
    });

    PointsRecord.associate = function (models) {
        PointsRecord.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user'
        });
        PointsRecord.belongsTo(models.RecycleOrder, {
            foreignKey: 'order_id',
            as: 'recycle_order'
        });
    };

    return PointsRecord;
};